package com.media.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

/**
 * <P>
 *        Mybatis-Plus 配置
 * </p>
 */
@Configuration
@MapperScan("com.media.mapper")
public class MybatisPlusConfig implements MetaObjectHandler {
   /**
    * 定义分页拦截器
    */
   @Bean
   public MybatisPlusInterceptor mybatisPlusInterceptor() {
      MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
      interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
      return interceptor;
   }


   /**
    * 新增数据填充
    * @param metaObject
    */
   @Override
   public void insertFill(MetaObject metaObject) {
      this.strictInsertFill(metaObject, "createDate", LocalDateTime.class, LocalDateTime.now());
      this.strictInsertFill(metaObject, "changeDate", LocalDateTime.class, LocalDateTime.now());

   }

   /**
    * 修改数据填充
    * @param metaObject
    */
   @Override
   public void updateFill(MetaObject metaObject) {
      this.strictInsertFill(metaObject, "changeDate", LocalDateTime.class, LocalDateTime.now());
   }
}